import 'package:flutter/material.dart';

main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "list demo",
      home: MyHomePage(
        title: "首页",
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool empty = false;
  int total = 30;

  final _controller = ScrollController();
  final List<String> items = [];

  _addItems() async {
    await Future.delayed(Duration(seconds: 2));
    setState(() {
      items.addAll(List.generate(10, (i) => "Item ${i + 1}"));
    });
  }

  @override
  Widget build(BuildContext context) {
    // print('_MyHomePageState build');
    return Scaffold(
        // appBar: AppBar(
        //   title: GestureDetector(
        //     child: Text(widget.title),
        //     onTap: () {
        //       if (!empty) {
        //         // animateTo动画跳转
        //         _controller.animateTo(-20,
        //             duration: const Duration(seconds: 2), curve: Curves.ease);
        //       }
        //     },
        //   ),
        // ),
        body: RefreshIndicator(
            onRefresh: () => Future.delayed(const Duration(seconds: 3)),
            // 创建滚动视窗，可以添加多个滚动组件
            child: CustomScrollView(
              slivers: [
                // 希望appbar能有其他效果，可以使用sliverappbar
                const SliverAppBar(
                    title: Text("sliver appbar"),
                    // pinned: true, // 固定在顶部
                    floating: true, // 往回划时能将appbar显示出来
                    snap: true, // 轻轻往回划都能将appbar显示出来，需要和float搭配使用
                    expandedHeight: 300, // 往下滑的可扩展高度
                    stretch: true, // 拉到最大高度后还能有一个回弹效果
                    flexibleSpace: FlexibleSpaceBar(
                      // 设置下拉后的空间内容
                      background: FlutterLogo(size: 50),
                      title: Text("FlutterLogo"),
                      collapseMode: CollapseMode.parallax, // 收起时候的效果
                      stretchModes: [], // 展开时的效果
                    )),
                // 将普通组件转为sliver
                const SliverToBoxAdapter(
                  child: FlutterLogo(size: 60),
                ),
                const SliverToBoxAdapter(
                  child: FlutterLogo(size: 60),
                ),
                // GridView的原生方法
                SliverGrid(
                    delegate: SliverChildBuilderDelegate((context, index) {
                      return Container(
                        height: 40,
                        color: Colors.primaries[index % 18],
                      );
                    }, childCount: 40),
                    gridDelegate:
                        const SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 4)),
                // listView的原生方法
                SliverList.builder(
                  itemCount: 20,
                  itemBuilder: (context, index) {
                    // print(index);
                    return Container(
                      height: 40,
                      color: Colors.primaries[index % 18],
                    );
                  },
                ),
                // 设置每个item的固定高度
                SliverFixedExtentList.builder(
                  itemBuilder: (context, index) {
                    return Container(
                      height: 40, // 这个高度就失效咯
                      color: Colors.primaries[index % 18],
                    );
                  },
                  itemExtent: 100,
                  itemCount: 50,
                ),
                // 每个item的高度固定，但不确定的情况
                SliverPrototypeExtentList.builder(
                    itemCount: 10,
                    itemBuilder: (context, index) {
                      return Container(
                        height: 40, // 这个高度就失效咯
                        color: Colors.primaries[index % 18],
                      );
                    },
                    // 传入一个item模型,会以这个模型的高度为基准
                    prototypeItem: Container(height: 50)),
                // 类似pageView 填满整个页面
                SliverFillViewport(
                    delegate: SliverChildBuilderDelegate((context, index) {
                  // print("page view$index");
                  return Container(
                    color: Colors.primaries[index % 18],
                    child: const Placeholder(),
                  );
                }, childCount: 3)),
                // 大小会固定为剩余视窗的大小
                const SliverFillRemaining(
                  child: Center(
                    child: CircularProgressIndicator(),
                  ),
                ),
                SliverToBoxAdapter(
                  child: Container(
                    height: 100,
                    color: Colors.red,
                  ),
                )
              ],
            )));
  }
}
